import numpy as np
import pandas as pd
from sklearn.metrics.ranking import roc_auc_score
import sklearn.preprocessing as prep
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
import os
import json
from Xg_Utils.params_run_submit import *
from sklearn.cross_validation import train_test_split
from numpy import random
from Ensemble_Utils.optimize_weights import *
import time


def test_random_shuffle():
    a = np.random.random((4,3))
    print a
    np.random.shuffle(a)
    print a
#test_random_shuffle()

def array_index():
    a = np.asarray([0,0,1,1])
    b = np.asarray([1,2,3,4])
    c = b[a==1]
    print a,b,c
#array_index()

def count_ratio():
    train_y = pd.read_csv('C:\DC_data' + '/train_y.csv')
    train_y = np.asarray(train_y,dtype=np.int8)
    print len(train_y[train_y==1]),len(train_y[train_y==0])
#count_ratio()

def sklearn_auc():
    a = np.asarray([1,1,1,1,1])
    b = np.asarray([0.,0.,1.,1.,1.])
    print roc_auc_score(b,a)
#sklearn_auc()

def dict_test():
    class_weight = dict([(i, 1) for i in range(2)])
    class_weight[1] = 2
    print class_weight
#dict_test()

def log_matrix():
    a = np.ones((4,3))
    print a,np.log(a)
#log_matrix()

def test_sklearn_prep():
    a = np.random.random((20,2)) * 10
    plt.subplot(1,3,1)
    plt.plot(a)
    plt.subplot(1,3,2)
    plt.plot(prep.scale(a))
    plt.subplot(1,3,3)
    plt.plot(prep.normalize(a))
    plt.show()
#test_sklearn_prep()

def test_PCA():
    X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
    X = np.random.random((10,6))
    print X
    pca = PCA(n_components=2)
    pca.fit(X)
    print pca.explained_variance_
    print pca.explained_variance_ratio_
    print pca.components_
#test_PCA()

def test_transpose():
    a = np.random.random((4,5))
    print a
    print a.transpose()
    print a.transpose().transpose()
#test_transpose()

def check_value_negtive():
    data_folder = 'C:\DC_data'
    train_X = pd.read_csv(data_folder + '/train_x.csv')
    X = np.asarray(train_X)
    print sum(sum(X==-1))
    print sum(sum(X==-2))
    print sum(sum(X==1))
    print sum(sum(X==2))
    print sum(sum(X<0))
    print sum(sum(X<-2))
    print X[X<-2]
#check_value_negtive()

def test_func_in_list():
    for e in [test_transpose(),check_value_negtive()]:
        print e
#test_func_in_list()

def test_os_path():
    print os.path
#test_os_path()

def test_save_to_json():
    performence = {
        'train_auc':0,
        'val_auc':1
    }
    params = params_XGBoost()
    performence['train_auc'] = 0.1
    performence['val_auc'] = 0.2

    re = [params,performence]
    f = open('test_j.json','wb')
    meta_json = json.dumps(re, default=lambda o: o.__dict__, indent=4)
    f.write(meta_json)
    f.close()
#test_save_to_json()

def test_random_train_val():
   x = np.asarray(([1,2],[3,4],[5,6],[7,8]))
   y = np.asarray([1,2,3,4])
   X, y, val_X, val_y = train_test_split(x,y,test_size=0.5,random_state=18)
   print X
   print y
   print val_X
   print val_y
#test_random_train_val()

#print random.randint(1,100)/100.

def test_read_json():
    file_path = 'Xg_metadata/bagging_forward_selection_of_learners/meta_data.json'
    #meta_data = json.load(file_path)
    meta_data = open(file_path)
    meta_data = json.load(meta_data)
    print meta_data['individual_model_performance'][1]
#test_read_json()

def test_np_clip():
    np.clip()

def test_np_array():
    a = np.zeros((4,))
    b = np.ones((4,))
    print a + b
    print (a + b)/len(a) + 1
    print (a + b)/(len(a) + 1)
#test_np_array()
"""
for c in  [1,2,3,4,5]:
    print c
"""

def test_max_index():
    a = []
    for i in range(5):
        a.append(i)
    a = np.asarray(a)
    i = np.argmax(a)
    print np.argmax(a)
    a_temp = a
    a_temp[i] = 0
    print np.argmax(a_temp)
#test_max_index()

def test_pearson():
    #a = np.random.random((2,4))
    a = np.asarray([0.1,0.1,0.2,0.2])
    b = np.asarray([0.1,0.1,0.2,0.3])
    c = np.asarray([1,1,3,4])
    d = np.vstack((a,b,c))
    print d
    print np.corrcoef(d)
#test_pearson()

def test_sleep():
    print '1'
    time.sleep(2)
    print '2'
#print test_sleep()

def test_continue():
    a = 0
    while True:
        if True:
            a += 1
            if a >= 5:
                print a
                break
#test_continue()

def test_pearson_via_csv():
    """
    f1 = pd.read_csv('Xg_metadata/bagging_forward_selection_of_learners/1_result.csv')
    f2 = pd.read_csv('Xg_metadata/bagging_forward_selection_of_learners/1_result.csv')
    """
    f1 = pd.read_csv('Xg_metadata/random_search_the_best_params_via_cv/result_on_all.csv')
    f2 = pd.read_csv('Xg_metadata/random_search_the_best_params_via_cv/result_on_submit.csv')
    a = np.asarray(f1.drop(['uid'],axis=1)).transpose()
    b = np.asarray(f2.drop(['uid'],axis=1)).transpose()

    c = np.vstack((a,b))
    print c.shape
    print np.corrcoef(c)
test_pearson_via_csv()

def test_pearson_via_csv_all():
    flag = 0
    folder = 'Xg_metadata/bagging_forward_selection_of_learners'
    for file in os.listdir(folder):
        if file.endswith('.csv'):
            flag += 1
            if flag == 1:
                f = pd.read_csv(folder + '/' + file)
                m = np.asarray(f.drop(['uid'],axis=1)).transpose()
            else:
                f = pd.read_csv(folder + '/' + file)
                v = np.asarray(f.drop(['uid'],axis=1)).transpose()
                #print v.shape
                m = np.vstack((m,v))
    print m.shape
    R = np.corrcoef(np.asarray(m))
    print R
#test_pearson_via_csv_all()

def test_list_to_array():
    a = []
    for i in range(5):
        a.append(i)
    a = np.asarray(a)
    print a
#test_list_to_array()

